package com.wwg.springboard;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.wwg.widget.impl.SpringBoardView;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private SpringBoardMediator mSpringBoardMediator;
    private SpringBoardView mSpringBoardView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSpringBoardView = findViewById(R.id.subscribed);
        mSpringBoardMediator = new SpringBoardMediator(mSpringBoardView);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_more) {
            List<String> records = UserActionHistoryProxy.getInstance().getRecord();
            if (records.size() > 0) {
                ArrayAdapter<String> adapter = new ArrayAdapter<>(
                        this,
                        android.R.layout.simple_spinner_item,
                        records);
                ListView listView = new ListView(this);
                listView.setAdapter(adapter);

                BottomSheetDialog dialog = new BottomSheetDialog(this);
                dialog.setContentView(listView);
                dialog.show();
            }

            return true;
        } else if (id == R.id.clear) {
            UserActionHistoryProxy.getInstance().clear();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        if (mSpringBoardView.onBackPressed()) {
            return;
        }

        super.onBackPressed();
    }
}